package com.chance.web.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chance.common.constant.RedisConstant;
import com.chance.common.utils.RedisImagesAddUtil;
import com.chance.model.entity.GraphInfo;
import com.chance.model.enums.ItemType;
import com.chance.web.admin.service.GraphInfoService;
import com.chance.web.admin.mapper.GraphInfoMapper;
import com.chance.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author HUANG
* @description 针对表【graph_info(图片信息表)】的数据库操作Service实现
* @createDate 2024-10-30 12:33:34
*/
@Service
public class GraphInfoServiceImpl extends ServiceImpl<GraphInfoMapper, GraphInfo>
    implements GraphInfoService{

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Override
    public List<String> getRemoveUrl() {
        return graphInfoMapper.getRemoveUrl();
    }

    @Override
    public void removeUrl(List<String> imgUrls) {
        graphInfoMapper.removeUrl(imgUrls);
    }


    /**
     * 保存图片列表
     * @param graphVoList 图片列表
     * @param id 图片所有id
     */
    public void saveBathById(List<GraphVo> graphVoList, ItemType itemType ,Long id) {
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo, graphInfo);
            graphInfo.setItemType(itemType);
            graphInfo.setItemId(id);
            graphInfos.add(graphInfo);
            String fileName = RedisImagesAddUtil.parseUrl(graphVo.getUrl());
            RedisImagesAddUtil.imageAddRedis(RedisConstant.DB_IMAGES_REDIS, fileName);
        });
        super.saveBatch(graphInfos);
    }
}




